//
//  PersonInformationViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/20.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "PersonInformationViewController.h"
#import "UserInformationView.h"
#import "GenderPickerVIew.h"
#import "ChooseBirthdayView.h"
@interface PersonInformationViewController ()< UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (nonatomic, strong) UserInformationView *userView;//用户设置的视图
@property (nonatomic, strong) GenderPickerVIew *pickerView;
@property (nonatomic, strong) ChooseBirthdayView *birthdayView;
@property (nonatomic, strong) UIView *pickerDownView;
@end

@implementation PersonInformationViewController


- (void)setNavBar{
    self.navigationItem.title = @"个人资料";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 50, 25);
    [button setTitle:@"保存" forState:UIControlStateNormal];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];
    button.block = ^(){
        [self save];
    };
    self.navigationItem.rightBarButtonItem = right;
}

//点击保存的方法
- (void)save
{
    if (![[userManager getUsrID] isEqualToString:@" "]) {
        NSLog(@"保存");
        //保存用户头像
        [userManager saveuserHeadImage:self.userView.headImageView.image];
        //保存用户昵称
        [userManager saveuserName:self.userView.nameTextField.text];
        //保存用户的生日
        [userManager saveuserBirthday:self.userView.ChooseBirthday.text];
        //保存用户的性别
        [userManager saveuserGender:self.userView.changeGenderLabel.text];
        //保存用户的签名
        [userManager saveuserPersonalSignature:self.userView.PersonalSignatureTextView.text];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBar];
    
    //创建滚动视图
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.scrollerView.contentSize = CGSizeMake(kWidth, 1.2*kHeight);
    self.scrollerView.showsVerticalScrollIndicator = YES;
    
    //自定义视图
    _userView = [[UserInformationView alloc] initWithFrame:self.view.bounds];
    [self.scrollerView addSubview:_userView];
    [self.view addSubview:self.scrollerView];
    
    //给改变头像添加轻拍手势
    _userView.changeImageLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap: )];
    [_userView.changeImageLabel addGestureRecognizer:tapGesture];
    
    //给性别添加手势
    _userView.changeGenderLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGender = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGender)];
    [self.userView.changeGenderLabel addGestureRecognizer:tapGender];
    
    //给出生年月添加手势
    UITapGestureRecognizer *tapBirthday = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBirthday)];
    _userView.ChooseBirthday.userInteractionEnabled = YES;
    
    [_userView.ChooseBirthday addGestureRecognizer:tapBirthday];
  
    
}


//轻拍头像选择头像
- (void) tap:(UITapGestureRecognizer *)tapGesture{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //如果有相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
            pickerVC.delegate = self;
            pickerVC.allowsEditing = YES;
            
            [pickerVC presentViewController:pickerVC animated:YES completion:nil];
        }
 
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];

}

//从相册中获取头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.userView.headImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark --------改变性别---------
//轻拍选择性别手势
-(void) tapGender{
   //创建pickerview
    self.pickerView = [[GenderPickerVIew alloc] initWithFrame:CGRectMake(0, kHeight -300, kWidth, 300)];
    self.pickerView.backgroundColor = [UIColor orangeColor];
    [self.scrollerView addSubview:self.pickerView];
    
    //回调完成的方法
    __weak PersonInformationViewController *perVC = self;
    self.pickerView.finishButton.block = ^{
        [perVC clickFinish];
    };
    
    //将pickerview下面再放一层view防止滑动scroller时下面的picker跟着滑动
    _pickerDownView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch: )];
    [_pickerDownView addGestureRecognizer:tap];
    [_pickerDownView addSubview:self.pickerView];
    [self.view addSubview:_pickerDownView];

    //value是中心点在滑动
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.y"];
    basic.fromValue = @(kHeight);
    basic.toValue = @(kHeight - 150);
    basic.duration = 0.5;
    basic.repeatCount = 1;
    [self.pickerView.layer addAnimation:basic forKey:nil];
    


}
//实现再次点击屏幕下面的选择框回收
- (void)touch:(UITapGestureRecognizer *)tap{

    UIView *backView = tap.view;
    [UIView animateWithDuration:0.3 animations:^{
        [UIView setAnimationRepeatCount:1];
        _pickerDownView.frame = CGRectMake(0, kHeight, kWidth, 300);
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
    }];

}


//点击完成的按钮实现的方法
- (void)clickFinish {
    self.userView.changeGenderLabel.text = self.pickerView.gender;
    [UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionRepeat) animations:^{
        [UIView setAnimationRepeatCount:1];
        _pickerDownView.frame = CGRectMake(0, kHeight, kWidth, 300);
    } completion:nil];
}

#pragma mark --------点击 出生日期-----------
//轻拍生日 时完成的方法
- (void) tapBirthday{
    
    if (self.userView.ChooseBirthday.text.length != 0) {
      
        NSArray *array = [self.userView.ChooseBirthday.text componentsSeparatedByString:@"-"];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(center) name:@"生日" object:array];
      
    }
    
    
    [UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionRepeat) animations:^{
        [UIView setAnimationRepeatCount:1];
        self.birthdayView = [[ChooseBirthdayView alloc] initWithFrame:CGRectMake(0, kHeight - 300, kWidth, 300)];
        self.birthdayView.backgroundColor = [UIColor orangeColor];
    } completion:nil];
  
    //回调完成的方法
    __weak PersonInformationViewController *perVC = self;
    self.birthdayView.finishButton.block = ^{
        [perVC clickFinishBirthday];
    };
    
    //将pickerview下面再放一层view防止滑动scroller时下面的picker跟着滑动
    _pickerDownView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchBirthday )];
    [_pickerDownView addGestureRecognizer:tap];
    [_pickerDownView addSubview:self.birthdayView];
    [self.view addSubview:_pickerDownView];
    

    
}
//再次点击 选择出生日期的框回去
- (void)touchBirthday {
    [UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionRepeat) animations:^{
        [UIView setAnimationRepeatCount:1];
        _pickerDownView.frame = CGRectMake(0, kHeight, kWidth, 300);
    } completion:nil];
}

- (void)clickFinishBirthday{
    NSString *string = [self.birthdayView.years stringByAppendingString:[NSString stringWithFormat:@"-%@", self.birthdayView.month]];
    self.userView.ChooseBirthday.text = [string stringByAppendingString:[NSString stringWithFormat:@"-%@", self.birthdayView.day]];
    [UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionRepeat) animations:^{
        [UIView setAnimationRepeatCount:1];
        _pickerDownView.frame = CGRectMake(0, kHeight, kWidth, 300);
    } completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
